package com.fare.taluo.core.domain.entity.crawl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.OneToMany;

import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.filter.Filter;

import com.dayatang.domain.InstanceFactory;
import com.fare.taluo.base.ddd.domain.BaseHBaseEntity;
import com.fare.taluo.base.ddd.domain.annontation.HBaseColumn;
import com.fare.taluo.core.domain.crawl.CrawlTaskModel;
import com.fare.taluo.core.domain.repository.IFreightRepository;
import com.fare.taluo.vo.core.HBaseFilterVO;
import com.fare.taluo.vo.core.PlaneInfo;
import com.foreveross.crawl.domain.freight.AgentEntity;
import com.foreveross.crawl.domain.freight.CabinEntity;
import com.foreveross.crawl.domain.freight.PlaneInfoEntity;
import com.foreveross.crawl.domain.freight.PriceEntity;
import com.foreveross.crawl.domain.freight.ReturnTripEntity;
import com.foreveross.crawl.domain.freight.TransitchangeEntity;
/**
 * 抓取到的航班信息模型
 * <p>该模型对象，为了兼容以前的存储结构保留了原有列存储定义名称,以及一些冗余属性
 * @author xiangsf 2013-2-6
 *
 */
@javax.persistence.Table(name="CRAWL_PLANE_FREIGHT")
public class FreightPriceEntity extends BaseHBaseEntity {
	//	list
	//public final static HBaseQueryFilter FILTER_0 = new HBaseQueryFilter("planeinfo:carierKey","planeinfo:carrierName");

	/**
	 * 
	 */
	private static final long serialVersionUID = 4601633868236154958L;
	/**
	 * 运营商简称英文，如：MU
	 */
	@HBaseColumn(qualifier="carrierKey",family="planeinfo")
	private String carrierKey;

	/**
	 * 运营商简称中文，如：南航
	 */
	@HBaseColumn(qualifier="carrierName",family="planeinfo")
	private String carrierName;

	/**
	 * 运营商全名，如：中国海南航空公司
	 */
	@HBaseColumn(qualifier="carrierFullName",family="planeinfo")
	private String carrierFullName;

	/**
	 * 航班批次日期。如:2012-05-19,格式:yyyy-MM-dd
	 */
	@HBaseColumn(qualifier="flightBatchDate",family="planeinfo")
	private String flightBatchDate;

	/**
	 * 航班起飞时间
	 */
	@HBaseColumn(qualifier="flightBatchStartTime",family="planeinfo", format="yyyyMMddHHmmss")
	private Date startTime;

	/**
	 * 航班预计到达时间
	 */
	@HBaseColumn(qualifier="flightBatchEndTime",family="planeinfo", format="yyyyMMddHHmmss")
	private Date endTime;

	/**
	 * 航班号，如： MU3540
	 */
	@HBaseColumn(qualifier="flightId",family="planeinfo")
	private String flightId;

	/**
	 * 目的地三字码
	 */
	@HBaseColumn(qualifier="toCity",family="planeinfo")
	private String toCity;
	/**
	 * 目的地名称
	 */
	@HBaseColumn(qualifier="toCityName",family="planeinfo")
	private String toCityName;
	/**
	 * 区域码
	 */
	@HBaseColumn(qualifier="areaCode",family="planeinfo")
	private String areaCode;
	/**
	 * 区域名称
	 */
	@HBaseColumn(qualifier="areaName",family="planeinfo")
	private String areaName;
	/**
	 * 起飞地三字码
	 * 
	 */
	@HBaseColumn(qualifier="fromCity",family="planeinfo")
	private String fromCity;
	/**
	 * 起飞地
	 * 
	 */
	@HBaseColumn(qualifier="fromCityName",family="planeinfo")
	private String fromCityName;

	/**
	 * 最低价格
	 */
	@HBaseColumn(qualifier="lowerPrice",family="planeinfo")
	private Double lowerPrice;

	/**
	 * 最低价代理商名称
	 */
	@HBaseColumn(qualifier="agentName",family="planeinfo")
	private String agentName;

	/**
	 * 最高价格
	 */
	@HBaseColumn(qualifier="highPrice",family="planeinfo")
	private Double highPrice;
	/**
	 * 附加的hbase应用值，用于生成rowkey,以便于应用根据该值统计
	 * <p>实际上使用下面的"抓取渠道ID"也可，但基于应用已完成部分功能，补充上该值
	 */
	@HBaseColumn(qualifier="grabChannelNum",family="planeinfo")
	private String attachHbaseKey;
	/**
	 * 抓取渠道ID
	 * */
	@HBaseColumn(qualifier="grabChannelId",family="planeinfo")
	private Long grabChannelId;
	/**
	 * 抓取渠道名称
	 */
	@HBaseColumn(qualifier="grabChannel",family="planeinfo")
	private String grabChannel;
	/**
	 * 外键，对应表CRAWL_PF_SOURCE_DATA的rowkey
	 * 
	 */
	@HBaseColumn(qualifier="SOURCE_ROWKEY",family="planeinfo")
	private String sourceRowkey;
	
	/**
	 * 飞机类型
	 */
	@HBaseColumn(qualifier="type",family="planeinfo")
	private String flightType = "";
	/**
	 * 创建时间
	 */
	 @HBaseColumn(qualifier="grabDate",family="planeinfo", format="yyyyMMddHHmmssSSS")
	private Date createTime = new Date();
	/**
	 * 代理商信息
	 */
	@OneToMany
	@HBaseColumn(qualifier="agents",family="agentInfos")
	private Set<AgentEntity> agents = new HashSet<AgentEntity>();
	/**
	 * 仓位信息
	 */
	@OneToMany
	@HBaseColumn(qualifier="cabins",family="planeinfo")
	private Set<CabinEntity> cabins = new HashSet<CabinEntity>();
	/**
	 * 价格除了仓位外，还分多个种类，比如燃油费，附加税费
	 */
	@OneToMany
	@HBaseColumn(qualifier="prices",family="priceInfos")
	private Set<PriceEntity> prices = new HashSet<PriceEntity>();
	
	/**
	 * 中转
	 */
	@OneToMany
	@HBaseColumn(qualifier="transit",family="planeinfo")
	private List<TransitchangeEntity> transit = new ArrayList<TransitchangeEntity>();
	/**
	 * 返程
	 */
	private List<ReturnTripEntity> returnTrip = new ArrayList<ReturnTripEntity>();
	
	
	public FreightPriceEntity(){}
	
	public FreightPriceEntity(CrawlTaskModel task){
		this.areaCode = task.getAreaCode();
		this.areaName = task.getAreaName();
		this.flightBatchDate = task.getFlightDate();
		this.fromCity = task.getFromCity();
		this.fromCityName = task.getFromCityName();
		this.grabChannel = task.getGrabChannel();
		this.grabChannelId = task.getGrabChannelId();
		this.toCity = task.getToCity();
		this.toCityName = task.getToCityName();
		this.attachHbaseKey = task.getAttachHbaseKey();
	}
	
	public String getAttachHbaseKey() {
		return attachHbaseKey;
	}

	public void setAttachHbaseKey(String attachHbaseKey) {
		this.attachHbaseKey = attachHbaseKey;
	}

	/**
	 * 生成航班数据的hbase主键
	 * 抓取日期(年月日时)_渠道_区域ID_起点(使用三字码)_终点(使用三字码)_起飞时间(年月日时)_航班ID_5位随即数
     *	10位 +    		  5位 +2位 + 3位   +        	3位      + 				10位+   		  8位 +    		5位
	 */
	@Override
	public String generateRowKey() {
		//如2013012318
		return new StringBuilder().append(DateFormatUtils.format(this.getCreateTime(), "yyyyMMddHH"))
		.append(StringUtils.leftPad(String.valueOf(this.getAttachHbaseKey()), 5, '0'))
		.append(StringUtils.leftPad(this.getAreaCode(), 2, '0'))
		.append(this.getFromCity())
		.append(this.getToCity())
		.append(DateFormatUtils.format(this.getStartTime(), "yyyyMMddHH"))
		.append(StringUtils.rightPad(this.getFlightId(), 8, '0'))
		.append(StringUtils.leftPad(String.valueOf(RandomUtils.nextInt(99999)), 5, '0')).toString();
	}

	public IFreightRepository getPlaneInfoRepository() {
		return InstanceFactory.getInstance(IFreightRepository.class);
	}
	public String getCarrierKey() {
		return carrierKey;
	}
	public void setCarrierKey(String carrierKey) {
		this.carrierKey = carrierKey;
	}
	public String getCarrierName() {
		return carrierName;
	}
	public void setCarrierName(String carrierName) {
		this.carrierName = carrierName;
	}
	public String getCarrierFullName() {
		return carrierFullName;
	}
	public void setCarrierFullName(String carrierFullName) {
		this.carrierFullName = carrierFullName;
	}
	
	public String getFlightBatchDate() {
		return flightBatchDate;
	}

	public void setFlightBatchDate(String flightBatchDate) {
		this.flightBatchDate = flightBatchDate;
	}

	public Date getStartTime() {
		return startTime;
	}
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	public Date getEndTime() {
		return endTime;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	
	public String getFlightId() {
		return flightId;
	}

	public void setFlightId(String flightId) {
		this.flightId = flightId;
	}

	public String getToCity() {
		return toCity;
	}
	public void setToCity(String toCity) {
		this.toCity = toCity;
	}
	public String getToCityName() {
		return toCityName;
	}
	
	public String getSourceRowkey() {
		return sourceRowkey;
	}

	public void setSourceRowkey(String sourceRowkey) {
		this.sourceRowkey = sourceRowkey;
	}

	public void setToCityName(String toCityName) {
		this.toCityName = toCityName;
	}
	public String getFromCity() {
		return fromCity;
	}
	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}
	public String getFromCityName() {
		return fromCityName;
	}
	public void setFromCityName(String fromCityName) {
		this.fromCityName = fromCityName;
	}
	public Double getLowerPrice() {
		return lowerPrice;
	}
	public void setLowerPrice(Double lowerPrice) {
		this.lowerPrice = lowerPrice;
	}
	public String getAgentName() {
		return agentName;
	}
	public void setAgentName(String agentName) {
		this.agentName = agentName;
	}
	public Double getHighPrice() {
		return highPrice;
	}
	public void setHighPrice(Double highPrice) {
		this.highPrice = highPrice;
	}
	public Long getGrabChannelId() {
		return grabChannelId;
	}
	public void setGrabChannelId(Long grabChannelId) {
		this.grabChannelId = grabChannelId;
	}
	public String getGrabChannel() {
		return grabChannel;
	}
	public void setGrabChannel(String grabChannel) {
		this.grabChannel = grabChannel;
	}
	
	public String getFlightType() {
		return flightType;
	}

	public void setFlightType(String flightType) {
		this.flightType = flightType;
	}

	public Set<AgentEntity> getAgents() {
		return agents;
	}
	public void setAgents(Set<AgentEntity> agents) {
		this.agents = agents;
	}
	public Set<CabinEntity> getCabins() {
		return cabins;
	}
	public void setCabins(Set<CabinEntity> cabins) {
		this.cabins = cabins;
	}
	public Set<PriceEntity> getPrices() {
		return prices;
	}
	public void setPrices(Set<PriceEntity> prices) {
		this.prices = prices;
	}
	
	public String getAreaCode() {
		return areaCode;
	}
	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}
	public String getAreaName() {
		return areaName;
	}
	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public List<TransitchangeEntity> getTransit() {
		return transit;
	}

	public void setTransit(List<TransitchangeEntity> transit) {
		this.transit = transit;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("FreightPriceEntity [carrierKey=");
		builder.append(carrierKey);
		builder.append(", carrierName=");
		builder.append(carrierName);
		builder.append(", carrierFullName=");
		builder.append(carrierFullName);
		builder.append(", flightDate=");
		builder.append(flightBatchDate);
		builder.append(", startTime=");
		builder.append(startTime);
		builder.append(", endTime=");
		builder.append(endTime);
		builder.append(", flightNo=");
		builder.append(flightId);
		builder.append(", toCity=");
		builder.append(toCity);
		builder.append(", toCityName=");
		builder.append(toCityName);
		builder.append(", areaCode=");
		builder.append(areaCode);
		builder.append(", areaName=");
		builder.append(areaName);
		builder.append(", fromCity=");
		builder.append(fromCity);
		builder.append(", fromCityName=");
		builder.append(fromCityName);
		builder.append(", lowerPrice=");
		builder.append(lowerPrice);
		builder.append(", agentName=");
		builder.append(agentName);
		builder.append(", highPrice=");
		builder.append(highPrice);
		builder.append(", attachHbaseKey=");
		builder.append(attachHbaseKey);
		builder.append(", grabChannelId=");
		builder.append(grabChannelId);
		builder.append(", grabChannel=");
		builder.append(grabChannel);
		builder.append(", sourceRowkey=");
		builder.append(sourceRowkey);
		builder.append(", flightType=");
		builder.append(flightType);
		builder.append(", createTime=");
		builder.append(createTime);
		builder.append("]");
		return builder.toString();
	}
	
	public FreightPriceEntity(PlaneInfoEntity p) {
		this.carrierKey = p.getCarrierKey();
		this.carrierName = p.getCarrierName();
		this.carrierFullName = p.getCarrierFullName();
		this.flightBatchDate = p.getFlightDate();
		this.startTime = p.getStartTime();
		this.endTime = p.getEndTime();
		this.flightId = p.getFlightNo();
		this.toCity = p.getToCity();
		this.toCityName = p.getToCityName();
		this.areaCode = p.getAreaCode();
		this.areaName = p.getAreaName();
		this.fromCity = p.getFromCity();
		this.fromCityName = p.getFromCityName();
		this.lowerPrice = p.getLowerPrice();
		this.agentName = p.getAgentName();
		this.highPrice = p.getHighPrice();
		this.attachHbaseKey = p.getAttachHbaseKey();
		this.grabChannelId = p.getGrabChannelId();
		this.grabChannel = p.getGrabChannel();
		this.sourceRowkey = p.getSourceRowkey();
		this.flightType = p.getType();
		this.createTime = p.getCreateTime();
		this.agents = p.getAgents();
		this.cabins = p.getCabins();
		this.prices = p.getPrices();
		this.transit = p.getTransit();
	}
	
	public List<ReturnTripEntity> getReturnTrip() {
		return returnTrip;
	}

	public void setReturnTrip(List<ReturnTripEntity> returnTrip) {
		this.returnTrip = returnTrip;
	}

	public List<FreightPriceEntity> findByConditions(String tableName,String startRowKey ,String endRowKey) throws Exception{
		return getPlaneInfoRepository().findByConditions(tableName,startRowKey, endRowKey);
	}
	public List<PlaneInfo> findByConditionsToPlaneInfo(String tableName,
			String startRowKey, String endRowKey,List<Filter> filters){
		return getPlaneInfoRepository().findByConditionsToPlaneInfo(tableName, startRowKey, endRowKey,filters);
	}
	public List<PlaneInfo> findByConditionsToPlaneInfo(String tableName,
			String startRowKey, String endRowKey, List<HBaseFilterVO> voList,
			List<Filter> filterList) {
		return getPlaneInfoRepository().findByConditionsToPlaneInfo(tableName,startRowKey, endRowKey, voList, filterList);
	}

	@Override
	public boolean isNew() {
		return getId() == null ;
	}

	@Override
	public boolean existed() {
		if (isNew()) {
			return false;
		}
		return getBaseEntityRepository().exists(getClass(), getId());
	}

	@Override
	public boolean notExisted() {
		return ! existed();
	}
	
	public List<PlaneInfo> findByCustomFilters(String tableName,
			String startRowKey, String endRowKey, List<HBaseFilterVO> voList,
			List<Filter> filterList){
		return getPlaneInfoRepository().findByCustomFilters( tableName,startRowKey,  endRowKey,  voList,
				filterList);
	}


	
	public static void main(String[] args) {
		 Class<?> demo=null;
	        try{
	            demo=Class.forName("com.fare.taluo.core.domain.entity.crawl.FreightPriceEntity");
	        }catch (Exception e) {
	            e.printStackTrace();
	        }
	        //取得父类
	        Class<?> temp=demo.getSuperclass();
	        
	        System.out.println("继承的父类为：   "+temp.getName());
	}
	@Override
	public boolean existed(String propertyName, Object propertyValue) {
		// TODO Auto-generated method stub
				return false;
	}
	
}
